package com.three.lab.widget;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.three.lab.R;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class SearchView extends LinearLayout {

    private String hint = "请输入关键词";
    private EditText et_input;
    private ImageView iv_del;

    private OnSearchChangeListener searchChangeListener;

    public SearchView(Context context) {
        super(context);
        initView(context,null);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    public void setSearchListener(OnSearchChangeListener searchListener){
        this.searchChangeListener = searchListener;
    }

    public void setSearchText(String searchText){
        if(TextUtils.isEmpty(searchText)) return;
        et_input.setText(searchText);
        et_input.setSelection(searchText.length());
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_search,this,true);
        et_input = view.findViewById(R.id.et_input);
        iv_del = view.findViewById(R.id.iv_del);
        setListener();
    }
    private void setListener() {
        et_input.setOnKeyListener(new View.OnKeyListener() {//点击搜索按钮监听事件
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //是否是回车键
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //隐藏键盘
                    ((InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(((Activity)getContext()).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    et_input.clearFocus();
                    //搜索
                    if(searchChangeListener != null){
                        searchChangeListener.onSearchChanged(et_input.getText().toString().trim());
                    }
                }
                return false;
            }
        });

        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(searchChangeListener != null){
                    searchChangeListener.onTextChanged(s.toString().trim());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString().trim())) {
                    iv_del.setVisibility(View.VISIBLE);
                } else {
                    iv_del.setVisibility(View.GONE);
                }
            }
        });
        iv_del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_input.setText("");
            }
        });
    }

    public interface OnSearchChangeListener{

        void onSearchChanged(String keyWord);

        void onTextChanged(String keyWord);

    }
}
